﻿using ApiDoc.Admin.Model.Api;
using ApiDoc.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WQDSkin.Forms.Component;
using WQDSkin.Forms.Controls;

namespace ApiDoc.Admin.View.Api
{
    /// <summary>
    /// frmInterfaceEdit.xaml 的交互逻辑
    /// </summary>
    public partial class frmInterfaceEdit : BasePage
    {
        private HttpClientApi httpClient;
        
        public frmInterfaceEdit()
        {
            InitializeComponent();
        }
         
        public async override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            ApiModel model = this.GetApiInfo;

            if (model.SN == 0)
            {
                model.Url = await this.httpClient.FullPath(model.FKSN) + "/";
            }
              
            if (model.SN > 0)
            {
                this.LoadParamList();
                this.LoadStepList();
            }
        }
         
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            this.httpClient = new HttpClientApi();

            this.cbxMeuthod.ItemsSource = GLConfig.Method;
            this.cbxSerializeType.ItemsSource = GLConfig.SerializeType;
            this.cbxExecuteType.ItemsSource = GLConfig.ExecuteType;
            this.cbxDataType.ItemsSource = GLConfig.DataBaseType;

            this.cbxDataBase.ItemsSource = GLConfig.DataBase;
            this.cbxDataBase.SelectedIndex = 0;
            this.cbxCommandType.ItemsSource = GLConfig.CommandType;
            this.cbxCommandType.SelectedIndex = 0;
        }

        #region 接口信息

        private void chkIsTran_Checked(object sender, RoutedEventArgs e)
        {
            //this.chkJms.Visibility = this.chkIsTran.IsChecked == true ? Visibility.Visible : Visibility.Hidden;
            //if (this.chkJms.Visibility == Visibility.Hidden)
            //{
            //    this.chkJms.IsChecked = false;
            //}
        }

        private async void btnSaveApi_Click(object sender, RoutedEventArgs e)
        {
            ApiModel model = (ApiModel)this.DataContext;

            ///接口信息
            if (this.Verifiy(this.frmApi))
            {
                this.DataContext = await this.httpClient.InterfaceSave(model);

                MessageBox.Show("保存成功!");
            }

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            ApiModel model = (ApiModel)this.DataContext;
            base.Contain.CallBack(model);
        }

        #endregion

        #region 接口参数

        private async void LoadParamList()
        {
           ApiModel apiInfo = GetApiInfo;
            this.dgvParam.ItemsSource = await this.httpClient.ParamList(apiInfo.SN);
        }

        private void btnParamAdd_Click(object sender, RoutedEventArgs e)
        {
            frmInterfaceParamEdit frmParam = new frmInterfaceParamEdit(); 
            ParamModel paramModel = new ParamModel();
            paramModel.FKSN = this.GetApiInfo.SN;
            frmParam.DataContext = paramModel;
            frmParam.WindowStartupLocation = WindowStartupLocation.CenterScreen;//显示位于当前屏幕的中心位置
            if (frmParam.ShowDialog() == true)
            {
                this.LoadParamList();
            };
        } 
        private void btnParamEdit_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgvParam.SelectedValue == null)
            {
                MessageBox.Show("请选择接口参数");
                return;
            }
            ParamModel paramModel = this.dgvParam.SelectedValue as ParamModel;

            frmInterfaceParamEdit frmParam = new frmInterfaceParamEdit(); 
            frmParam.DataContext = paramModel;
            frmParam.WindowStartupLocation = WindowStartupLocation.CenterScreen;//显示位于当前屏幕的中心位置
            if (frmParam.ShowDialog() == true)
            {
                this.LoadParamList();
            };
        } 
        private async void btnParamDelete_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgvParam.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择接口参数");
                return;
            }

            if (MessageBox.Show("请确认是否要删除", "删除提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                List<int> deleteList = new List<int>();
                foreach (ParamModel model in this.dgvParam.SelectedItems)
                {
                    deleteList.Add(model.SN);
                }
                int result = await this.httpClient.ParamDelete(deleteList);
                if (result > 0)
                {
                    this.LoadParamList();
                }
            }
        }
         
        private void btnParamFind_Click(object sender, RoutedEventArgs e)
        {
            this.LoadParamList();
        }

        #endregion

        private ApiModel GetApiInfo
        {
            get
            {
                ApiModel apiInfo = new ApiModel();
                if (this.DataContext != null)
                {
                    apiInfo = (ApiModel)this.DataContext;
                }

                return apiInfo;
            }
        }


        #region 步骤

        private async void LoadStepList()
        {
            this.dgvStep.ItemsSource = await this.httpClient.StepList(this.GetApiInfo.SN);
        }

        private void btnStepAdd_Click(object sender, RoutedEventArgs e)
        {
            frmStepEdit win = new frmStepEdit();
            FlowStepModel model = new FlowStepModel();
            model.FKSN = this.GetApiInfo.SN;
            win.DataContext = model;
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;//显示位于当前屏幕的中心位置
            if (win.ShowDialog() == true)
            {
                this.LoadStepList();
            };
        }

        private void btnStepEdit_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgvStep.SelectedValue == null)
            {
                MessageBox.Show("请选择步骤");
                return;
            }

            FlowStepModel model = this.dgvStep.SelectedValue as FlowStepModel; 
            frmStepEdit frmParam = new frmStepEdit();
            frmParam.DataContext = model;
            frmParam.WindowStartupLocation = WindowStartupLocation.CenterScreen;//显示位于当前屏幕的中心位置
            if (frmParam.ShowDialog() == true)
            {
                this.LoadStepList();
            };
        }

        private async void btnStepDelete_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgvStep.SelectedValue == null)
            {
                MessageBox.Show("请选择接口参数");
                return;
            }

            if (MessageBox.Show("请确认是否要删除", "删除提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                int SN = ((FlowStepModel)this.dgvStep.SelectedValue).SN;
                int result = await this.httpClient.StepDelete(SN);
                if (result > 0)
                {
                    this.LoadStepList();
                }
            }
        }

        private void btnStepFind_Click(object sender, RoutedEventArgs e)
        {
            this.LoadStepList();
        }
         
        private async void dgvStep_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FlowStepModel model = this.dgvStep.SelectedValue as FlowStepModel;
            StepModel Step = await this.httpClient.StepGet(model.SN);

            this.frmStep.DataContext = Step;
            this.tviParam.ItemsSource = Step.StepParamList; 
        }


        private async void btnStepSave_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgvStep.SelectedValue == null)
            {
                MessageBox.Show("请选择步骤数据");
                return;
            }
            FlowStepModel model = this.frmStep.DataContext as FlowStepModel;
            int result = await this.httpClient.StepSaveCmdText(model);
            if (result > 0)
            {
                MessageBox.Show("保存成功!");
            }
        }

        #endregion

        #region 步骤参数

        private async void LoadStepParamList()
        {
            if (this.dgvStep.SelectedValue != null)
            {
                FlowStepModel stepInfo = this.dgvStep.SelectedValue as FlowStepModel;
                List<FlowStepParamModel> list = await this.httpClient.StepParamList(stepInfo.SN);
                this.tviParam.ItemsSource = list; 
            } 
        }
 
        private void btnStepParamAdd_Click(object sender, RoutedEventArgs e)
        {
            if (this.dgvStep.SelectedValue == null)
            {
                MessageBox.Show("请选择步骤");
                return;
            }
            frmStepParamEdit win = new frmStepParamEdit();
            FlowStepParamModel model = new FlowStepParamModel(); 
            FlowStepModel step = this.dgvStep.SelectedValue as FlowStepModel;
            model.FKSN = step.SN;
            win.DataContext = model;
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;//显示位于当前屏幕的中心位置
            if (win.ShowDialog() == true)
            {
                this.LoadStepParamList();
            };
        }

        private void btnStepParamEdit_Click(object sender, RoutedEventArgs e)
        {
            if (this.tvStepParam.SelectedItem == null)
            {
                MessageBox.Show("请选择步骤参数");
                return;
            }

            //WQDTreeViewItem tviSelected = this.tvStepParam.SelectedItem as WQDTreeViewItem;
            FlowStepParamModel model = this.tvStepParam.SelectedItem as FlowStepParamModel;
             
            frmStepParamEdit frmParam = new frmStepParamEdit();
            frmParam.DataContext = model;
            frmParam.WindowStartupLocation = WindowStartupLocation.CenterScreen;//显示位于当前屏幕的中心位置
            if (frmParam.ShowDialog() == true)
            {
                this.LoadStepParamList();
            };
        }

        private async void btnStepParamDelete_Click(object sender, RoutedEventArgs e)
        {
            if (this.tvStepParam.SelectedItem == null)
            {
                MessageBox.Show("请选择项目参数");
                return;
            }
             
            if (MessageBox.Show("请确认是否要删除","删除提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            { 
                //WQDTreeViewItem tviSelected = this.tvStepParam.SelectedItem as WQDTreeViewItem;
                FlowStepParamModel model = this.tvStepParam.SelectedItem as FlowStepParamModel;
                int SN = model.SN;

                int result = await this.httpClient.StepParamDelete(SN);
                if (result > 0)
                {
                    this.LoadStepParamList();
                }
            } 
        }

        private void btnStepParamFind_Click(object sender, RoutedEventArgs e)
        {
            LoadStepParamList();
        }

        #endregion

    }
}
